package com.xdwanj.filter;

import com.mysql.jdbc.StringUtils;
import com.xdwanj.pojo.User;
import com.xdwanj.servlet.PageServlet;
import com.xdwanj.util.ConstantsStr;
import com.xdwanj.util.UrlUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String url = null;
        System.out.println("进入UserFilter...");
        User user = null;
        String pagePosition = null;
        String uid = null;
        String coid = null;

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        pagePosition = request.getParameter(PageServlet.POSITION);
        user = (User) request.getSession().getAttribute(ConstantsStr.USER_SESSION);
        uid = request.getParameter(PageServlet.UID);

        if (!StringUtils.isNullOrEmpty(pagePosition)) { // 页面坐标不为空，有指定页面定位
            switch (pagePosition) {
                case PageServlet.USER:  // 去往用户页面
                    if (user == null) { // Session中没有User对象
                        url = request.getContextPath() + UrlUtil.getUrl(PageServlet.REQUEST_MAPPING, new String[]{PageServlet.POSITION, PageServlet.LOGIN});
                        System.out.println("url = " + url);
                        response.sendRedirect(url);
                        return;
                    } else if (!StringUtils.isStrictlyNumeric(uid)) { // Session中有User对象,并且url地址没有uid参数或着非有效数字
                        url = request.getContextPath() + UrlUtil.getUrl(PageServlet.REQUEST_MAPPING, new String[]{
                                PageServlet.POSITION, PageServlet.USER,
                                PageServlet.UID, user.getId().toString()});
                        System.out.println("url = " + url);
                        response.sendRedirect(url);
                        return;
                    }

                    break;
                case PageServlet.SHOP:  // 去往商城页面
                    if (user == null) { // Session中无User对象
                        url = request.getContextPath() + request.getContextPath() + UrlUtil.getUrl(PageServlet.REQUEST_MAPPING, new String[]{PageServlet.POSITION, PageServlet.LOGIN});
                        System.out.println("url = " + url);
                        response.sendRedirect(url);
                        return;
                    }

                    break;

                case PageServlet.LOGIN:  // 去往登陆页面
                    if (user != null) { // Session中有User对象
                        url = request.getContextPath() + UrlUtil.getUrl(PageServlet.REQUEST_MAPPING, new String[]{
                                PageServlet.POSITION, PageServlet.USER});
                        System.out.println("url = " + url);
                        response.sendRedirect(url);
                        return;
                    }
                    break;

                case PageServlet.COMMODITY: // 去往商品页面
                    if (user == null) {
                        url = request.getContextPath() + UrlUtil.getUrl(PageServlet.REQUEST_MAPPING, new String[]{PageServlet.POSITION, PageServlet.LOGIN});
                        System.out.println("url = " + url);
                        response.sendRedirect(url);
                        return;
                    }
                    break;

                default:
                    break;
            }

            // 其他不被限制的页面直接放行
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        // 页面坐标为空，送他去主页
        url = request.getContextPath() + UrlUtil.getUrl(PageServlet.REQUEST_MAPPING, new String[]{PageServlet.POSITION, PageServlet.INDEX});
        System.out.println("url = " + url);
        response.sendRedirect(url);
    }

    @Override
    public void destroy() {

    }
}
